package ms.jasim.model.editor.actions;

import ms.jasim.framework.IJasimAppContext;
import ms.jasim.model.JasimModel;
import ms.jasim.model.ModelObject;
import ms.jasim.model.editor.views.ConsoleView;
import ms.jasim.model.editor.views.SolutionListView;
import ms.jasim.model.presentation.JasimEditorAdvisor;
import ms.jasim.model.presentation.JasimEditorPlugin;
import ms.jasim.pddl.PddlPlanner;
import ms.utils.PerformanceTimer;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

public class RunPlannerAction implements IWorkbenchWindowActionDelegate, IEditorActionDelegate {

	private JasimModel jasimModel;
	private String modelFile;

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IWorkbenchWindow window) {
		// TODO Auto-generated method stub

	}

	@Override
	public void run(IAction action) {
		if (jasimModel != null) {
			JasimModel model = jasimModel;

			IJasimAppContext context = JasimEditorPlugin.INSTANCE.getRootContext();
			if (context.getService(ConsoleView.class) != null) {
				ConsoleView view = context.getService(ConsoleView.class);
				view.clearAll();
			} else
				JasimEditorAdvisor.WindowActionBarAdvisor.showConsoleView.run();

			PddlPlanner planner = new PddlPlanner(context);
			planner.setPddlModel(modelFile, model);
			planner.run();

			try {
				IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				SolutionListView view = (SolutionListView) activePage.showView(SolutionListView.ID);
				view.setInput(planner.getSolutions());
			} catch (PartInitException exception) {
				JasimEditorPlugin.INSTANCE.log(exception);
			}
			
			PerformanceTimer.dumpEntries();
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection instanceof TreeSelection) {
			TreeSelection ts = (TreeSelection) selection;
			Object first = ts.getFirstElement();
			
			this.jasimModel = null;
			
			if (first instanceof XMIResource) {
				EList<EObject> resList = ((XMIResource)first).getContents();
				this.modelFile = ((XMIResource)first).getURI().toFileString();
				this.jasimModel = resList.size() > 0 && resList.get(0) instanceof JasimModel ? (JasimModel) resList.get(0) : null;
			}
			else if (first instanceof ModelObject) {
				EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor(first);
				if (domain != null) {
					EList<Resource> list = domain.getResourceSet().getResources();
					EObject root = list.get(0).getContents().get(0);
					if (root instanceof JasimModel) {
						this.jasimModel = (JasimModel) root ;
						this.modelFile = list.get(0).getURI().toFileString();
					}
				}
			}
		}
		else 
			this.jasimModel = null;
		
		action.setEnabled(this.jasimModel != null);
	}

	
	@Override
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		// TODO Auto-generated method stub
		
	}

}
